﻿Imports System.Data.OleDb
'problem with page. Null dob 3 textboxes will not allow update to happen. sort this out asap.



Public Class UserProfileEdit
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            Dim sql As String = "SELECT * FROM UserProfile WHERE Username=@f20"

            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            conn.Open()

            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

            cmd.Parameters.AddWithValue("@f20", User.Identity.Name)
            Dim datareader = cmd.ExecuteReader()


            datareader.Read()

            If Not IsDBNull(datareader("Title")) Then ddltitle.SelectedValue = datareader("Title")
            If Not IsDBNull(datareader("FirstName")) Then txtfirstname.Text = datareader("FirstName")
            If Not IsDBNull(datareader("LastName")) Then txtlastname.Text = datareader("LastName")
            If Not IsDBNull(datareader("DOB")) Then txtage1.Text = datareader("DOB")
            If Not IsDBNull(datareader("Address1")) Then txtaddress1.Text = datareader("Address1")
            If Not IsDBNull(datareader("Address2")) Then txtaddress2.Text = datareader("Address2")
            If Not IsDBNull(datareader("Town")) Then txttown.Text = datareader("Town")
            If Not IsDBNull(datareader("City")) Then txtcity.Text = datareader("City")
            If Not IsDBNull(datareader("Postcode")) Then txtpostcode.Text = datareader("Postcode")
            If Not IsDBNull(datareader("TelNo")) Then txttelephoneno.Text = datareader("TelNo")
            If Not IsDBNull(datareader("AboutMe")) Then txtdescription.Text = datareader("AboutMe")
            If Not IsDBNull(datareader("X6CarsDriven")) Then txtexperience.Text = datareader("X6CarsDriven")
            If Not IsDBNull(datareader("FavouriteColour")) Then txtfavcarcolour.Text = datareader("FavouriteColour")
            If Not IsDBNull(datareader("MaxSpeedDriven")) Then txtmaxspeeddriven.Text = datareader("MaxSpeedDriven")
            If Not IsDBNull(datareader("PreferedFuel")) Then ddlpreferedFuel.SelectedValue = datareader("PreferedFuel")
            If Not IsDBNull(datareader("PreferedLitre")) Then txtlitre.Text = datareader("PreferedLitre")
            If Not IsDBNull(datareader("Gender")) Then ddlgender.SelectedValue = datareader("Gender")
            If Not IsDBNull(datareader("UserImage")) Then UserProfilePictureEdit.ImageUrl = datareader("UserImage")



        End If






    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles btnsubmit.Click




        Dim newFileName As String = ""

        If fupprofilepicture.HasFile Then

            newFileName = Guid.NewGuid().ToString() & getsuffix(fupprofilepicture.FileName)
            If fupprofilepicture.HasFile Then
                fupprofilepicture.SaveAs(Server.MapPath("UserProfileImages") & "/" & newFileName)
            End If
        End If

        Dim sql As String

        If fupprofilepicture.HasFile Then
            sql = "UPDATE UserProfile SET Title=@f2, FirstName=@f3, LastName=@f4, DOB=@f5, Address1=@f6, Address2=@f7, Town=@f8, City=@f9, Postcode=@f10, TelNo=@f11, AboutMe=@f12, X6CarsDriven=@f13, FavouriteColour=@f14, MaxSpeedDriven=@f15, PreferedFuel=@f16, PreferedLitre=@f17, Gender=@f18, UserImage=@f19 WHERE Username=@f20"

        Else
            sql = "UPDATE UserProfile SET Title=@f2, FirstName=@f3, LastName=@f4, DOB=@f5, Address1=@f6, Address2=@f7, Town=@f8, City=@f9, Postcode=@f10, TelNo=@f11, AboutMe=@f12, X6CarsDriven=@f13, FavouriteColour=@f14, MaxSpeedDriven=@f15, PreferedFuel=@f16, PreferedLitre=@f17, Gender=@f18 WHERE Username=@f20"
        End If

        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        conn.Open()

        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f2", ddltitle.SelectedValue)
        cmd.Parameters.AddWithValue("@f3", txtfirstname.Text)
        cmd.Parameters.AddWithValue("@f4", txtlastname.Text)
        cmd.Parameters.AddWithValue("@f5", txtage1.Text)
        cmd.Parameters.AddWithValue("@f6", txtaddress1.Text)
        cmd.Parameters.AddWithValue("@f7", txtaddress2.Text)
        cmd.Parameters.AddWithValue("@f8", txttown.Text)
        cmd.Parameters.AddWithValue("@f9", txtcity.Text)
        cmd.Parameters.AddWithValue("@f10", txtpostcode.Text)
        cmd.Parameters.AddWithValue("@f11", txttelephoneno.Text)
        cmd.Parameters.AddWithValue("@f12", txtdescription.Text)
        cmd.Parameters.AddWithValue("@f13", txtexperience.Text)
        cmd.Parameters.AddWithValue("@f14", txtfavcarcolour.Text)
        cmd.Parameters.AddWithValue("@f15", txtmaxspeeddriven.Text)
        cmd.Parameters.AddWithValue("@f16", ddlpreferedFuel.SelectedValue)
        cmd.Parameters.AddWithValue("@f17", txtlitre.Text)
        cmd.Parameters.AddWithValue("@f18", ddlgender.SelectedValue)
        If fupprofilepicture.HasFile Then cmd.Parameters.AddWithValue("@f19", "UserProfileImages/" & newFileName)
        cmd.Parameters.AddWithValue("@f20", User.Identity.Name)

        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()

        Response.Redirect("UserProfile.aspx")
    End Sub

    Function getsuffix(ByVal fn As String)
        Dim fileparts = fn.Split(".")
        Dim suffix = fileparts(fileparts.Length - 1)
        Return "." & suffix
    End Function

    Protected Sub txtexperience_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtexperience.TextChanged

    End Sub
End Class